package com.amap.map.network.handler;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.amap.api.location.AMapLocation;
import com.amap.map.network.entity.UploadData;
import com.amap.map.util.Configuration;
import com.amap.map.util.ToastUtil;
import com.amap.map.util.common.StringUtils;
import com.amap.map.util.locationSource.LocationSourceProxy;
import com.amap.map.util.network.HttpConnectionUtil;
import com.amap.map.util.network.HttpConnectionUtil.HttpConnectionCallback;
import com.amap.map.util.network.HttpConnectionUtil.HttpMethod;
import com.amap.map.util.network.RequestHead;

/****
 * 定位数据上报网络通信模块
 * @author liuxing
 *
 */
public class DataUploadHandler implements Runnable {
	
	private Map<String, String> params = new HashMap<String, String>() ;
	private LocationSourceProxy proxy ;
	
	public DataUploadHandler( Double locationx,Double locationy ,LocationSourceProxy proxy ){
		 params.put("reqHead", JSON.toJSONString( new RequestHead(101, new Date().toString() )));
		 UploadData data = new UploadData( locationx , locationy ,Configuration.IEMI_NAME ,Configuration.MOBILE_NAME  ) ;
         try {
			params.put("bodyParam", JSON.toJSONString(data) );
		} catch (Exception e) {
		}
         this.proxy = proxy ;
	}

	@Override
	public void run() {
		//自定义类,封装了GET/POST方法,而且同样也封装了同步跟异步的方法
		HttpConnectionUtil conn = new HttpConnectionUtil();
		conn.asyncConnect( Configuration.NETWORK_RUL , params, HttpMethod.POST,
			new HttpConnectionCallback(){
				@Override
				public void execute(String response)
				{
					if( !StringUtils.isEmpty(response) ){
						proxy.callBackMethod(response);
					}
				}
			}
		);
	}
	
	public static void main(String[] args) {
	//	new Thread( new DataUploadHandler( 23.42432,112.34243 ) ).start();
	}

}
